package com.jic.market.impl.rest.discountamount.processor;

import com.jic.market.common.util.Money;
import com.jic.market.entity.GoodsSku;
import com.jic.market.entity.PromotionGiftItemEntity;
import com.jic.market.entity.PromotionRuleEntity;
import com.jic.market.entity.coupon.CouponConfig;
import com.jic.market.entity.coupon.CouponDetail;
import com.jic.market.impl.rest.discountamount.model.PromotionAmountModel;
import com.jic.market.impl.rest.discountamount.po.GoodsPo;
import com.jic.market.mapper.PromotionGiftItemEntityMapper;
import com.jic.market.mapper.PromotionRuleEntityMapper;
import com.jic.market.mapper.coupon.CouponConfigMapper;
import com.jic.market.mapper.coupon.CouponDetailMapper;
import com.jic.market.response.rest.discount.GiftDetail;
import com.jic.product.after.api.ProductListApi;
import com.jic.product.after.request.GoodsPageQueryRequest;
import com.jic.product.after.response.ProductStoreInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 赠送-满量赠
 * @author yinhaijun
 * @date: 2020/4/7
 */
@Service("GiftFullNumAmountProcessor")
public class GiftFullNumAmountProcessor extends AmountProcessor {

    @Autowired
    private PromotionRuleEntityMapper promotionRuleEntityMapper;

    @Resource
    private ProductListApi productListApi;

    @Autowired
    private PromotionGiftItemEntityMapper promotionGiftItemEntityMapper;

    @Autowired
    private CouponDetailMapper couponDetailMapper;
    @Autowired
    private CouponConfigMapper couponConfigMapper;

    @Override
    public void shoppingQuery(PromotionAmountModel promotionAmountModel, GoodsPo goodsPo, String thirdUserId, Object request){
        // 获取所有的规则数据
        List<PromotionRuleEntity> promotionRuleEntityList = promotionRuleEntityMapper.queryByPromotionId(Long.parseLong(promotionAmountModel.getPromotionId()));
        // 安条件匹配选出符合规则
        PromotionRuleEntity promotionRuleEntity = super.getFlightPromotionRuleEntity(new BigDecimal(goodsPo.getNum()), promotionRuleEntityList);
        if(null == promotionRuleEntity){
            promotionAmountModel.fillNoDiscount(goodsPo);
            return;
        }
        // 最终匹配成功需要乘的倍数
        Integer overlayAppNum = getBeiSu(promotionAmountModel, goodsPo.getTotalAmount(), promotionRuleEntity);

        // 查询出所有的赠送商品
         PromotionGiftItemEntity promotionGiftItemEntityQ = new PromotionGiftItemEntity();
        promotionGiftItemEntityQ.setPromotionId(Long.parseLong(promotionAmountModel.getPromotionId()));
        promotionGiftItemEntityQ.setPromotionRuleId(promotionRuleEntity.getId());
        List<PromotionGiftItemEntity> promotionGiftItemEntityList = promotionGiftItemEntityMapper.findAll(promotionGiftItemEntityQ);
        if(null != promotionGiftItemEntityList && !promotionGiftItemEntityList.isEmpty()){
            List<GiftDetail> giftDetailList = new ArrayList<>();
            Long finalOverlayAppNum = overlayAppNum.longValue();
            promotionGiftItemEntityList.forEach(promotionGiftItemEntity -> {
                GiftDetail giftDetail = new GiftDetail();
                giftDetail.setGiftType(promotionGiftItemEntity.getGiftType());
                giftDetail.setCouponPromotionId(promotionGiftItemEntity.getCouponId());
                giftDetail.setThirdProductCode(promotionGiftItemEntity.getMpCode());
                if(null == promotionGiftItemEntity.getGiftNum()){
                    giftDetail.setNum(1L * finalOverlayAppNum);
                }else {
                    giftDetail.setNum(promotionGiftItemEntity.getGiftNum() * finalOverlayAppNum);
                }
                giftDetailList.add(giftDetail);
            });
            promotionAmountModel.setRuleData(giftDetailList);
        }
        promotionAmountModel.setDeleteModel(false);
        super.shoppingQuery(promotionAmountModel, goodsPo, thirdUserId, request);
    }

    private Integer getBeiSu(PromotionAmountModel promotionAmountModel, BigDecimal condition, PromotionRuleEntity promotionRuleEntity) {
        Integer overlayAppNum = 1;
        if(promotionAmountModel.getOverlayAppFlag()){
            BigDecimal divideInt = Money.divideInt(condition, new BigDecimal(promotionRuleEntity.getConditionValue()));
            if(divideInt.compareTo(new BigDecimal(promotionAmountModel.getOverlayAppNum())) == 1){
                overlayAppNum = promotionAmountModel.getOverlayAppNum();
            }else {
                overlayAppNum = divideInt.intValue();
            }
        }
        return overlayAppNum;
    }

    @Override
    public Boolean isCondition(PromotionAmountModel promotionAmountModel, GoodsPo goodsPo, String thirdUserId) {
        if(!checkCommonLimitRule(promotionAmountModel.getPromotionId(), goodsPo.getSkuId(), goodsPo.getNum(), thirdUserId)){
            return false;
        }
        return true;
    }

    @Override
    public BigDecimal promotionDiscountTotalAmount(PromotionAmountModel promotionAmountModel, GoodsPo goodsPo, Object request) {
        List<GiftDetail> giftDetailList = (List<GiftDetail>) promotionAmountModel.getRuleData();
        if(null == giftDetailList || giftDetailList.isEmpty()){
            return BigDecimal.ZERO;
        }
        // 计算赠品总额，为优惠总额
        BigDecimal promotionDiscountTotalAmount = BigDecimal.ZERO;
        for(GiftDetail giftDetail : giftDetailList){
            // 赠品处理
            if(giftDetail.getGiftType().equals(1)){
//                GoodsSku goodsSkuQ = new GoodsSku();
//                goodsSkuQ.setThirdSkuId(giftDetail.getThirdProductCode());
//                List<GoodsSku> goodsSkuList = goodsSkuMapper.query(goodsSkuQ);
                List<ProductStoreInfo> goodsSkuList = productListApi.queryProductBySpuList(new GoodsPageQueryRequest() {{
                    setGoodsCode(giftDetail.getThirdProductCode());
                }});
                if(null != goodsSkuList && !goodsSkuList.isEmpty()){
                    BigDecimal skuPrice = Money.multiply(goodsSkuList.get(0).getSalePriceWithTax(), new BigDecimal(giftDetail.getNum()));
                    promotionDiscountTotalAmount = promotionDiscountTotalAmount.add(skuPrice);
                }
            }else if(giftDetail.getGiftType().equals(4) || giftDetail.getGiftType().equals(5)){
                CouponDetail couponDetail = new CouponDetail();
//                couponDetail.setCouponCode(giftDetail.getCouponCode());
                List<CouponDetail> couponDetailList = couponDetailMapper.query(couponDetail);
                if(null != couponDetailList && !couponDetailList.isEmpty()){
                    CouponConfig couponConfig = couponConfigMapper.queryByPromotionId(couponDetailList.get(0).getPromotionId());
                    if(null != couponConfig){
                        BigDecimal skuPrice = Money.multiply(couponConfig.getCouponAmount(), new BigDecimal(giftDetail.getNum()));
                        promotionDiscountTotalAmount = promotionDiscountTotalAmount.add(skuPrice);
                    }
                }
            }
        }
        return promotionDiscountTotalAmount;
    }

}
